/**
 * File		: Cube.java
 * Subject	: Represents a single cube
 * Author	: Isaac Parenteau (locus2k@gmail.com)
 * Date		: April 16th, 2011
 * Note 	: Code was borrowed from Google and modified to fit the projects use
 * Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package src.project.cube;

import java.util.ArrayList;

import src.project.util.Vertex;


public class Cube extends Shape{

    public static final int Bottom = 0;
    public static final int Front = 1;
    public static final int Left = 2;
    public static final int Right = 3;
    public static final int Back = 4;
    public static final int Top = 5;
    
    public boolean Selected = false;
    
    public ArrayList<Integer> CurrentLayer = new ArrayList<Integer>();
    
    private int index;
	
    /**
     * Constructor
     * @param world the world to draw to
     * @param left left coordinate of the cube
     * @param bottom bottom coordinate of the cube
     * @param back back coordinate of the cube
     * @param right right coordinate of the cube
     * @param top top coordinate of the cube
     * @param front front coordinate of the cube
     * @param index index of the cube
     */
	public Cube(World world, float left, float bottom, float back, 
			float right, float top, float front, int index){
		super(world);
		
		this.index = index;
		
		//Creates the vertexes of the cube
		Vertex leftBottomBack = addVertex(left, bottom, back);
		Vertex rightBottomBack = addVertex(right, bottom, back);
		Vertex leftTopBack = addVertex(left, top, back);
		Vertex rightTopBack = addVertex(right, top, back);
		Vertex leftBottomFront = addVertex(left, bottom, front);
		Vertex rightBottomFront = addVertex(right, bottom, front);
		Vertex leftTopFront = addVertex(left, top, front);
		Vertex rightTopFront = addVertex(right, top, front);
		
        // vertices are added in a clockwise orientation (when viewed from the outside)
        // bottom
        addFace(new Face(leftBottomBack, leftBottomFront, rightBottomFront, rightBottomBack));
        // front
        addFace(new Face(leftBottomFront, leftTopFront, rightTopFront, rightBottomFront));
        // left
        addFace(new Face(leftBottomBack, leftTopBack, leftTopFront, leftBottomFront));
        // right
        addFace(new Face(rightBottomBack, rightBottomFront, rightTopFront, rightTopBack));
        // back
        addFace(new Face(leftBottomBack, rightBottomBack, rightTopBack, leftTopBack));
        // top
        addFace(new Face(leftTopBack, rightTopBack, rightTopFront, leftTopFront));
	}
	
	/**
	 * Updates the cube to its current layer location
	 * @param layer
	 */
	public void updateCurrentLayer(int layer){
		CurrentLayer.add(layer);
	}
	
	/**
	 * Clears the cubes Current layers
	 */
	public void clearCurrentLayer(){
		CurrentLayer.clear();
	}
	
	/**
	 * Checks to see if the cube belongs to a certain layer
	 * @param layer the layer to test
	 * @return if the cube is in the layer
	 */
	public boolean isValidLayer(int layer){
		return CurrentLayer.contains(layer);
	}
	
	/**
	 * Prints the current layers the cube belongs to
	 * @return
	 */
	public String printCurrentLayer(){
		
		StringBuilder string = new StringBuilder();
		string.append(String.format("Cube %d is in layers ", index));
		
		for(int i : CurrentLayer)
			string.append(String.format("%d, ", i));
		
		return string.toString();
	}
	
	/**
	 * Gets the index of the cube 
	 * @return index
	 */
	public int getIndex() { return index; }
}
